home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / scripts / plotter.irt < prev    next >
Encoding:
Text File  |  1994-09-17  |  3.7 KB  |  136 lines

  1. #
  2. # A package to plot explicit curves and surfaces as polylines.
  3. #
  4. #
  5. #            Gershon Elber, October 1993.
  6. #
  7.  
  8. #
  9. # The functions to plot. Get x or x,y and returns y or z values.
  10. #
  11. plotfn2d = function(x):
  12.     return = x^3 - 5*x^2 + 4*x - 5;
  13.  
  14. plotfn3d = function(x, y):
  15.     return = x*x - y*y;
  16.  
  17. iritState("EchoSource", false);
  18.  
  19. #
  20. # These objects must be undefined druing definition of functions below:
  21. #
  22. if (thisobj("tr") != undef_type, free(tr));
  23.  
  24. #
  25. # Computes a polyline out of the plotfn2d function from paramter values
  26. # min to max in n steps.
  27. #
  28. plotfunc2d2poly = function(min, max, n):lst:t:
  29.     lst = nil():
  30.     for (t = min, (max - min) / (n - 1), max,
  31.     snoc(vector(t, plotfn2d(t), 0.0), lst)):
  32.     return = poly(lst, true);
  33.  
  34. #
  35. # Reposition the 2d polyline in the [-1..1] xy domain and plot it.
  36. #
  37. plotfunc2d = function(minx, maxx, n):pl:miny:maxy:i:v:tr:ax:sv:
  38.     pl = plotfunc2d2poly(minx, maxx, n):
  39.     color(pl, yellow):
  40.     attrib(pl, "width", 0.05):
  41.  
  42.     # find the Y domain of the function.
  43.     miny = 1e6:
  44.     maxy = -1e6:
  45.     for (i = 0, 1, 2, miny = miny+i):
  46.     return = pl:
  47.  
  48.     for (i = 0, 1, sizeof(pl) - 1,
  49.     v = coord(pl, i):
  50.     if (coord(v, 1) > maxy, maxy = coord(v, 1)):
  51.     if (coord(v, 1) < miny, miny = coord(v, 1))):
  52.  
  53.     # Make the axes
  54.     ax = poly(list(vector(min(minx, 0), 0, 0),
  55.                vector(max(maxx, 0), 0, 0)),
  56.           true) +
  57.      poly(list(vector(0, min(miny, 0), 0),
  58.            vector(0, max(maxy, 0), 0)),
  59.           true):
  60.     color(ax, red):
  61.     attrib(ax, "width", 0.02):
  62.  
  63.     # Map and display the polyline to the [-1..1] domain in both x and y.
  64.     tr = trans(vector(-(minx + maxx) / 2, -(miny + maxy) / 2, 0)) *
  65.      scale(vector(2 / (maxx - minx), 2 / (maxy - miny), 0)):
  66.     sv = view_mat:
  67.     view_mat = rotx(0):
  68.     viewobj(list(view_mat, return = list(pl, ax) * tr)):
  69.     printf("XDomain = [%lf %lf], YDomain = [%lf %lf]\\n",
  70.        list(minx, maxx, miny, maxy)):
  71.     view_mat = sv;
  72.  
  73. #
  74. # Computes polylines out of the plotfn3d function from parameter values
  75. # minx/y to maxx/y in n steps and m isolines for each size.
  76. #
  77. plotfunc3d2poly = function(minx, maxx, miny, maxy, n, m):lst:x:y:first:
  78.     first = true:
  79.     for (x = minx, (maxx - minx) / (m - 1), maxx,
  80.     lst = nil():
  81.     for (y = miny, (maxy - miny) / (n - 1), maxy,
  82.         snoc(vector(x, y, plotfn3d(x, y)), lst)):
  83.     if (first == true,
  84.         return = poly(lst, true):first = false,
  85.         return = return + poly(lst, true))):
  86.     for (y = miny, (maxy - miny) / (m - 1), maxy,
  87.     lst = nil():
  88.     for (x = minx, (maxx - minx) / (n - 1), maxx,
  89.         snoc(vector(x, y, plotfn3d(x, y)), lst)):
  90.     return = return + poly(lst, true));
  91.  
  92. #
  93. # Plot the 3d polylines in their original domain.
  94. #
  95. plotfunc3d = function(minx, maxx, miny, maxy, n, m):pl:minz:maxz:v:p:ax:i:j:
  96.     pl = plotfunc3d2poly(minx, maxx, miny, maxy, n, m):
  97.     color(pl, yellow):
  98.     attrib(pl, "width", 0.05):
  99.  
  100.     # find the Y domain of the function.
  101.     minz = 1e6:
  102.     maxz = -1e6:
  103.     for (i = 0, 1, sizeof(pl) - 1,
  104.         p = coord(pl, i):
  105.     for (j = 0, 1, sizeof(p) - 1,
  106.         v = coord(p, i):
  107.         if (coord(v, 2) > maxz, maxz = coord(v, 2)):
  108.         if (coord(v, 2) < minz, minz = coord(v, 2)))):
  109.  
  110.     # Make the axes
  111.     ax = poly(list(vector(min(minx, 0), 0, 0),
  112.                vector(max(maxx, 0), 0, 0)),
  113.           true) +
  114.      poly(list(vector(0, min(miny, 0), 0),
  115.            vector(0, max(maxy, 0), 0)),
  116.           true) +
  117.      poly(list(vector(0, 0, min(minz, 0)),
  118.            vector(0, 0, max(maxz, 0))),
  119.           true):
  120.     color(ax, red):
  121.     attrib(ax, "width", 0.02):
  122.  
  123.     # Plot the polylines to their original domain.
  124.     viewobj(return = list(pl, ax)):
  125.     printf("XDomain = [%lf %lf], YDomain = [%lf %lf], ZDomain = [%lf %lf]\\n",
  126.        list(minx, maxx, miny, maxy, minz, maxz));
  127.  
  128. iritState("EchoSource", true);
  129.  
  130. viewclear();
  131. fn = plotfunc2d(-1, 5, 50);
  132. pause();
  133.  
  134. viewclear();
  135. fn = plotfunc3d(-1, 1, -1, 1, 30, 10);
  136. pause();